package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsTopicSection;
import com.ruoyi.system.domain.news.dto.NewsTopicSectionDto;
import com.ruoyi.system.domain.news.query.NewsTopicSectionQuery;
import com.ruoyi.system.domain.news.vo.NewsTopicSectionVo;
import com.ruoyi.system.service.news.NewsTopicSectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsTopicSection")
@Api(value = "newsTopicSectionController", tags  = "专题专栏")
public class NewsTopicSectionController extends BaseController {

	@Resource
	private NewsTopicSectionService newsTopicSectionService;

    @ApiOperation(value = "新增/修改专题专栏信息", notes = "新增/修改专题专栏信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsTopicSectionDto newsTopicSectionDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsTopicSection newsTopicSection = new NewsTopicSection();
		BeanUtils.copyProperties(newsTopicSectionDto,newsTopicSection);
		if (newsTopicSection.getId()==null) {
			newsTopicSection.setCreateBy(loginUser.getUsername());
			newsTopicSection.setDeptId(loginUser.getDeptId());
			newsTopicSection.setCreateTime(new Date());
			newsTopicSection.setUpdateBy(loginUser.getUsername());
			newsTopicSection.setUpdateTime(new Date());
		} else {
			newsTopicSection.setUpdateBy(loginUser.getUsername());
			newsTopicSection.setUpdateTime(new Date());
		}
		boolean result = newsTopicSectionService.saveOrUpdate(newsTopicSection);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除专题专栏信息", notes = "删除专题专栏信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsTopicSectionService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个专题专栏信息", notes = "获取单个专题专栏信息")
	@GetMapping("/getById")
	public R<NewsTopicSectionVo> getById(@RequestParam(value = "id") Long id) {
		NewsTopicSectionQuery newsTopicSectionQuery = new NewsTopicSectionQuery();
		newsTopicSectionQuery.setId(id);
		List<NewsTopicSectionVo> newsTopicSectionList = newsTopicSectionService.getNewsTopicSectionList(newsTopicSectionQuery);
		if (newsTopicSectionList!=null&&newsTopicSectionList.size()>0) {
			return R.ok(newsTopicSectionList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取专题专栏信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsTopicSectionVo>> list(@RequestBody NewsTopicSectionQuery newsTopicSectionQuery) {
		List<NewsTopicSectionVo> newsTopicSectionList = newsTopicSectionService.getNewsTopicSectionList(newsTopicSectionQuery);
		return R.ok(new PageInfo<>(newsTopicSectionList));
	}
}
